use std::io;
use rand::prelude::*;
use std::cmp::Ordering;
fn main() {
    let secret_number: i32 = rand::thread_rng().gen_range(1..=100);
    // println!("神秘数字是{}", secret_number);
    println!("猜数!");
    println!("猜一个数字,1-100之间");

    loop {
        let mut guess: String = String::new();
        io::stdin().read_line(&mut guess).expect("无法读取行");
        
        // guess.pop();
        // 先将guess变量转换为整数类型
        let guess: i32 =  match guess.trim().parse() {
            // 解析正确时将数字返回
            Ok(num) => num,
            // 解析错误时，使用 continue 跳出本次循环，进行下一次循环
            Err(_) => {
                println!("输入错误,请重新输入");
                continue
            }
        };
        println!("你猜的数字是: {}", guess);
    
        match guess.cmp(&secret_number){
            Ordering::Less => println!("太小了"),
            Ordering::Greater => print!("太大了!"),
            Ordering::Equal => {
                println!("猜对了!");
                break;
            }
        }
    }
}
